package com.sxt.practiceapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sxt.practiceapplication.R;
import com.sxt.practiceapplication.entity.studyrecord.StudyRecord;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class StudyRecordListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final LayoutInflater inflater;
    private final List<StudyRecord> studyRecords;
    private static final int TYPE_TOP = 0x0000;
    private static final int TYPE_NORMAL = 0x0001;
    private ViewHolder.OnItemClickListener onItemClickListener;

    public StudyRecordListAdapter(Context context, List<StudyRecord> studyRecords) {
        inflater = LayoutInflater.from(context);
        this.studyRecords = studyRecords;
    }

    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_trace, parent, false));
    }

    @Override
    public void onBindViewHolder(@NotNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder itemHolder = (ViewHolder) holder;
        if (getItemViewType(position) == TYPE_TOP) {
            // 第一行头的竖线不显示
            itemHolder.tvTopLine.setVisibility(View.INVISIBLE);
            // 字体颜色加深
            itemHolder.tvAccessTime.setTextColor(0xff555555);
            itemHolder.tvCourseTitle.setTextColor(0xff555555);
            itemHolder.tvCourseDes.setTextColor(0xff555555);
            itemHolder.tvDot.setBackgroundResource(R.drawable.time_line_dot_first);
        } else if (getItemViewType(position) == TYPE_NORMAL) {
            itemHolder.tvTopLine.setVisibility(View.VISIBLE);
            itemHolder.tvAccessTime.setTextColor(0xff999999);
            itemHolder.tvCourseTitle.setTextColor(0xff999999);
            itemHolder.tvCourseDes.setTextColor(0xff999999);
            itemHolder.tvDot.setBackgroundResource(R.drawable.time_line_dot_normal);
        }

        StudyRecord studyRecord = studyRecords.get(position);
        itemHolder.bindHolder(studyRecord);

        itemHolder.llCourseInfo.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onClick(v, studyRecord);
            }
        });
        itemHolder.llCourseInfo.setOnLongClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onLongClick(v, studyRecord);
            }
            return true;
        });
    }

    @Override
    public int getItemCount() {
        return studyRecords.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TOP;
        }
        return TYPE_NORMAL;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView tvAccessTime;
        private final TextView tvTopLine;
        private final TextView tvDot;
        private final TextView tvCourseTitle;
        private final TextView tvCourseDes;
        public final View llCourseInfo;
        public StudyRecord studyRecord;

        public ViewHolder(View itemView) {
            super(itemView);
            tvAccessTime = itemView.findViewById(R.id.tvAccessTime);
            tvTopLine = itemView.findViewById(R.id.tvTopLine);
            tvDot = itemView.findViewById(R.id.tvDot);
            tvCourseTitle = itemView.findViewById(R.id.tv_course_title);
            tvCourseDes = itemView.findViewById(R.id.tv_course_des);
            llCourseInfo = itemView.findViewById(R.id.ll_cource_info);
        }

        public void bindHolder(StudyRecord studyRecord) {
            tvAccessTime.setText(studyRecord.studyTime.toString());
            tvCourseTitle.setText(studyRecord.course.courseTitle);
            tvCourseDes.setText(studyRecord.course.courseDes);
            this.studyRecord = studyRecord;
        }

        public interface OnItemClickListener {
            void onClick(View view, StudyRecord studyRecord);

            void onLongClick(View view, StudyRecord studyRecord);
        }
    }

    public void setOnItemClickListener(ViewHolder.OnItemClickListener listener) {
        onItemClickListener = listener;
    }
}